package bsts.controller;

import java.util.ArrayList;

import bsts.model.Feature;

public class FeatureController {

	private ArrayList<Feature> features;
	private IOController ioController;

	public FeatureController() {
		ioController = new IOController();
		this.features = ioController.RetrieveFeatures();
	}

	public boolean UpdateFeature(Feature feature) {
		for (int i = 0; i < features.size(); i++) {
			Feature comp = features.get(i);
			if (comp.getFeatureId() == feature.getFeatureId()) {
				// Remove from Arraylist
				features.remove(i);
				// Insert the update object to Arraylist
				features.add(feature);
				// Update file
				return ioController.UpdateFeatures(this.features);
			}
		}// end of loop
		return false;
	}// end of function

	public boolean AddFeature(Feature feature) {
		features.add(feature);
		return ioController.UpdateFeatures(this.features);
	}

	public Feature GetFeature(long featureId) {
		for (int i = 0; i < features.size(); i++) {
			Feature comp = features.get(i);
			if (comp.getFeatureId() == featureId) {
				return comp;
			}
		}// end of loop
		return null;
	}// end of function

	public ArrayList<Feature> getFeatures() {
		return features;
	}

	public void setFeatures(ArrayList<Feature> features) {
		this.features = features;
	}

	public Integer getCurrent_FeatureID() {
		Integer temp_FeatureID;
		ArrayList<Feature> tempFeatures = new ArrayList<Feature>();
		ioController = new IOController();

		tempFeatures = ioController.RetrieveFeatures();

		if (tempFeatures == null) {
			temp_FeatureID = new Integer(0);
		} else {
			temp_FeatureID = new Integer(10000 + tempFeatures.size());
		}

		return temp_FeatureID;
	}

	public Integer getNext_FeatureID() {
		return getCurrent_FeatureID() + 1;
	}

	public ArrayList<Feature> GetFeaturesByFeatureID(
			ArrayList<String> feature_ids) {
		ArrayList<Feature> filtered_features = new ArrayList<Feature>();

		for (int i = 0; i < features.size(); i++) {
			Feature feature = features.get(i);
			for (int x = 0; x < feature_ids.size(); x++) {
				if (String.valueOf(feature.getFeatureId()).equals(
						feature_ids.get(x))) {
					filtered_features.add(feature);
				}
			}

		}// end of loop
		return features;
	}
}
